package wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

// reduce阶段
// KEYIN, VALUEIN: 对应map阶段输出的key和value的类型
// KEYOUT, VALUEOUT: 输出的key和value的类型
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    private IntWritable valueOut = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum = 0;

        for (IntWritable value :
                values) {
            sum += value.get();
        }

        valueOut.set(sum);
        context.write(key, valueOut);
    }
}
